<?php namespace App\Models;

use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Poll
 *
 * @property integer $id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $name
 * @property string $slug
 * @property string $ended_at
 * @property boolean $numbers_per_ip
 * @property boolean $hours_per_ip
 * @property boolean $only_user
 * @property boolean $is_random
 * @property boolean $hide_category
 * @property string $banner_pic
 * @property string $background_pic
 * @property string $background_color
 * @property integer $works_count
 * @property boolean $locked
 * @property string $deleted_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereSlug($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereEndedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereNumbersPerIp($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereHoursPerIp($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereOnlyUser($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereIsRandom($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereHideCategory($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereBannerPic($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereBackgroundPic($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereBackgroundColor($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereWorksCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereLocked($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Poll whereDeletedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Works[] $works
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PollWorks[] $pollWorks
 */
class Poll extends Model
{
    use SoftDeletes;

    protected $table = 'polls';

    public $fillable = [
        'name',
        'slug',
        'ended_at',
        'numbers_per_ip',
        'hours_per_ip',
        'only_user',
        'is_random',
        'hide_category',
        'banner_pic',
        'background_pic',
        'background_color',
        'works_count',
    ];

    /**
     * 上传图片
     * @param Request $request
     */
    public function uploadPics(Request $request)
    {
        $picPath = public_path('uploads/poll');
        $hasFile = false;
        if ($request->hasFile('banner')) {
            try {
                $bannerPicFile = $request->file('banner');
                $extension = $bannerPicFile->getClientOriginalExtension();
                $fileName = "banner_pic_{$this->id}.{$extension}";
                $bannerPicFile->move($picPath, $fileName);
                $this->banner_pic = "upload/poll/{$fileName}";
                $hasFile = true;
            } catch (\Exception $e) {
                \Log::error('上传网投banner失败,' . $e->getMessage());
            }
        }

        if ($request->hasFile('background')) {
            try {
                $backgroundPicFile = $request->file('background');
                $extension = $backgroundPicFile->getClientOriginalExtension();
                $fileName = "background_pic_{$this->id}.{$extension}";
                $backgroundPicFile->move($picPath, $fileName);
                $this->background_pic = "upload/poll/{$fileName}";
                $hasFile = true;
            } catch (\Exception $e) {
                \Log::error('上传网投banner失败,' . $e->getMessage());
            }
        }

        if ($hasFile) {
            $this->save();
        }
    }

    public function works()
    {
        return $this->belongsToMany(Works::class, 'poll_works', 'poll_id', 'works_id');
    }
    
    public function pollWorks()
    {
        return $this->hasMany(PollWorks::class, 'poll_id');
    }
    
    public function pollNews()
    {
        return $this->hasMany(PollNews::class, 'poll_id');
    }
}